package cn.cxy.demo

import android.os.Bundle
import android.view.Gravity
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener { showDefaultToast("toast消息") }
        topButton.setOnClickListener { showToastOnTop("toast消息") }
        centerButton.setOnClickListener { showToastAtCenter("toast消息") }
    }

    /**
     * 显示默认toast，一般是在底部显示
     */
    private fun showDefaultToast(text: String) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
    }

    /**
     * 在顶部显示toast
     */
    private fun showToastOnTop(text: String) {
        val toast = Toast.makeText(this, text, Toast.LENGTH_SHORT)
        toast.setGravity(Gravity.TOP, 0, 0)
        toast.show()
    }

    /**
     * 在中间显示toast
     */
    private fun showToastAtCenter(text: String) {
        val toast = Toast.makeText(this, text, Toast.LENGTH_SHORT)
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()
    }
}